package controller;
import javax.servlet.http.Cookie;
import javax.servlet.*;
import javax.servlet.http.*;

import java.io.*;
import java.nio.charset.StandardCharsets;

import service.login_service;
import entity.user;
public class login_servlet extends HttpServlet{
	private static final long serialVersionUID = 1L;
	private login_service lg_service=new login_service();
	public login_servlet() {
		super();
	}
	public void destroy() {
		super.destroy(); 
	}
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		request.setCharacterEncoding("utf-8");
		HttpSession session= request.getSession();
		String requestURI = request.getRequestURI();
		user u=new user();
		String userAgent = request.getHeader("User-Agent");
		boolean is_mobile = false;
		String[] mobileKeywords = {"Android", "iPhone", "iPad", "Windows Phone"};
		for (String keyword : mobileKeywords) {
		    if (userAgent.contains(keyword)) {
		    	is_mobile = true;
		        break;
		    }
		}
		request.setAttribute("is_mobile", is_mobile);
		if (requestURI.equals("/teamfight_wiki/login_msg")){
			String username=request.getParameter("username");
			String password=request.getParameter("password");
			u.setPassword(password);
			u.setUser_name(username);
			u=lg_service.login(u);
			if (u.getType()>0){
				session.setAttribute("user", u);
				request.getRequestDispatcher("/home").forward(request,response);
			}
			else if(u.getType()==-1){
				request.setAttribute("reason", "无此用户");
				request.getRequestDispatcher("/account_manage/login_failure.jsp").forward(request,response);
			}
			else{
				request.setAttribute("reason", "密码错误");
				request.getRequestDispatcher("/account_manage/login_failure.jsp").forward(request,response);
			}
		}
		else if (requestURI.equals("/teamfight_wiki/sign_msg")){
			String username=request.getParameter("username");
			String password=request.getParameter("password");
			String security_question=request.getParameter("security_question");
			String security_answer=request.getParameter("security_answer");
			u.setPassword(password);
			u.setUser_name(username);
			u.setSecurity_question(security_question);
			u.setSecurity_answer(security_answer);
			boolean res=lg_service.sign(u);
			if(res){
				request.getRequestDispatcher("/account_manage/login.jsp").forward(request,response);
			}
			else{
				request.setAttribute("resaon", "已有同名用户");
				request.getRequestDispatcher("/account_manage/sign_failure.jsp").forward(request,response);
			}
		}
		else if (requestURI.equals("/teamfight_wiki/authentication")){
			String username=request.getParameter("username");
			u.setUser_name(username);
			u=lg_service.find(u);
			if(u.getType()==1){	
				session.setAttribute("username", username);	
				request.setAttribute("security_question", u.getSecurity_question());
				request.getRequestDispatcher("/account_manage/forget_password2.jsp").forward(request,response);
			}
			else if (u.getType()>1){
				request.setAttribute("reason", "管理账户请联系管理员更改");
				request.getRequestDispatcher("/account_manage/login_failure.jsp").forward(request,response);
			}
			else{
				request.setAttribute("reason", "无此用户");
				request.getRequestDispatcher("/account_manage/login_failure.jsp").forward(request,response);
			}
		}
		else if (requestURI.equals("/teamfight_wiki/renew_password")){
			String username=(String) session.getAttribute("username");
			String security_answer=request.getParameter("security_answer");
			u.setUser_name(username);
			u.setSecurity_answer(security_answer);
			if(lg_service.answer_correct(u)){
				request.getRequestDispatcher("/account_manage/forget_password3.jsp").forward(request,response);
			}
			else{
				request.setAttribute("reason", "保密问题答案错误");
				request.getRequestDispatcher("/account_manage/login_failure.jsp").forward(request,response);
			}
		}
		else if (requestURI.equals("/teamfight_wiki/new_password")){
			String username=(String) session.getAttribute("username");
			String password=request.getParameter("password");
			u.setPassword(password);
			u.setUser_name(username);
			lg_service.forget(u);
			request.getRequestDispatcher("/account_manage/home.jsp").forward(request,response);
		}
		else if (requestURI.equals("/teamfight_wiki/quit")){
			session.removeAttribute("user");	
			request.getRequestDispatcher("/").forward(request,response);
		}
		else{
			u=(user)session.getAttribute("user");
			if(u!=null){
				String name=u.getUser_name();
				request.setAttribute("name", name);
				request.getRequestDispatcher("/account_manage/home.jsp").forward(request,response);
				return;
			}
			request.getRequestDispatcher("/account_manage/home.jsp").forward(request,response);
		}
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request,response);
	}
}
